package org.deltaset.meta.internal;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Arrays;

/**
 *
 * @author dmakariev
 */
class BeanProperty {

    private final String fieldName;
    private final Class fieldType;
    private final Class fieldGenericClass;
    private final Annotation[] fieldAnnotations;

    public BeanProperty(final Field field) {
        this(field.getName(), field.getType(), field.getGenericType(), field.getAnnotations());

    }

    BeanProperty(String fieldName, Class fieldType, Type genericParameterType, Annotation[] fieldAnnotations) {
        this.fieldName = fieldName;
        this.fieldType = fieldType;
        this.fieldAnnotations = fieldAnnotations;
        this.fieldGenericClass = ReflectionUtils.extractGenericClass(genericParameterType);
    }   

    public Annotation[] getFieldAnnotations() {
        return fieldAnnotations;
    }

    public String getFieldName() {
        return fieldName;
    }

    public Class getFieldType() {
        return fieldType;
    }

    public Class getFieldGenericType() {
        return fieldGenericClass;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final BeanProperty other = (BeanProperty) obj;
        if ((this.fieldName == null) ? (other.fieldName != null) : !this.fieldName.equals(other.fieldName)) {
            return false;
        }
        if (this.fieldType != other.fieldType && (this.fieldType == null || !this.fieldType.equals(other.fieldType))) {
            return false;
        }
        if (this.fieldGenericClass != other.fieldGenericClass && (this.fieldGenericClass == null || !this.fieldGenericClass.equals(other.fieldGenericClass))) {
            return false;
        }
        if (!Arrays.deepEquals(this.fieldAnnotations, other.fieldAnnotations)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 37 * hash + (this.fieldName != null ? this.fieldName.hashCode() : 0);
        hash = 37 * hash + (this.fieldType != null ? this.fieldType.hashCode() : 0);
        hash = 37 * hash + (this.fieldGenericClass != null ? this.fieldGenericClass.hashCode() : 0);
        hash = 37 * hash + Arrays.deepHashCode(this.fieldAnnotations);
        return hash;
    }        
        
}
